// check if all reference js files existing
var references = new ECCIT.Script.References();
references.push(new ECCIT.Script.Reference("Namespace", "ECCIT.Script.Core.js"));
references.push(new ECCIT.Script.Reference("ECCIT.Script.Web.UI.WebControls.Style.Constants", "ECCIT.Script.Core.js"));
references.push(new ECCIT.Script.Reference("ECCIT.Script.Web.UI.WebControls.Style.DisplayMode", "ECCIT.Script.Core.js"));
references.push(new ECCIT.Script.Reference("ECCIT.Script.View.Configuration.Message.Constants", "ECCIT.Script.Core.js"));
references.validate();

// register namespace
Namespace.register("ECCIT.Script.Web.UI.WebControls");

var __g_StyleConstants = ECCIT.Script.Web.UI.WebControls.Style.Constants;
var __g_MessageConstants = ECCIT.Script.View.Configuration.Message.Constants;
var __g_DisplayMode = ECCIT.Script.Web.UI.WebControls.Style.DisplayMode;

/**********************************************************************************************/
/************************************Interface IControl****************************************/
/**********************************************************************************************/
ECCIT.Script.Web.UI.WebControls.IControl =	function(id)
{
	this.id = id;               // control id
	this.htmlControl = $('#' + id)[0];
	
	this.name = (Object.isNullOrEmpty(this.htmlControl)) ? "" : $('#' + id).attr('name').val();             // control name
	this.caption = "";          // control caption
	this.enabled = true;        // if control is enabled
	this.visible = true;        // if control is visible
	this.htmlControl = null;    // binding html control on web page
	this.mandatory = false;     // if control is mandatory
	this.displayMode = __g_DisplayMode.Edit;
	
	if (document.getElementById(id) != null && document.getElementById(id) != "undefined")
	{
	    this.htmlControl = document.getElementById(id);
	}
	
	if (typeof ECCIT.Script.Web.UI.WebControls.IControl._initialized == "undefined")
	{
	    var __this = ECCIT.Script.Web.UI.WebControls.IControl.prototype;
	    
	    // interface methods only for implementing in inherited classes
		__this.getSaveValue         = function(){};
		__this.setValue             = function(value){};
		__this.setEnabled           = function(enabled){};
		__this.setVisible           = function(visible){};
		__this.display              = function(value){};
		__this.validate             = function(){};
		__this.changeDisplayMode    = function(mode){};
		
		ECCIT.Script.Web.UI.WebControls.IControl._initialized = true;
	}
}

/*  BaseControl */
ECCIT.Script.Web.UI.WebControls.BaseControl = function(id)
{
    ECCIT.Script.Web.UI.WebControls.IControl.call(this, id);
}

/*  inherit from IControl   */
ECCIT.Script.Web.UI.WebControls.BaseControl.prototype = new ECCIT.Script.Web.UI.WebControls.IControl();
var __BaseControl = ECCIT.Script.Web.UI.WebControls.BaseControl.prototype;

/*  public methods inherited from IControl  */
__BaseControl.setEnabled = function(enabled){
    this.enabled = enabled;
};



/**********************************************************************************************/
/*  TextBox */
/**********************************************************************************************/

// TextBox constructor
ECCIT.Script.Web.UI.WebControls.TextBox = function (id)
{
	ECCIT.Script.Web.UI.WebControls.BaseControl.call(this, id);
}

// inherit from BaseControl
ECCIT.Script.Web.UI.WebControls.TextBox.prototype = new ECCIT.Script.Web.UI.WebControls.BaseControl();
var __TextBox = ECCIT.Script.Web.UI.WebControls.TextBox.prototype;

/* 
    public methods of TextBox 
*/

/*  setEnabled  */
__TextBox.overridedSetEnabled = __TextBox.setEnabled;
__TextBox.setEnabled = function(enabled){
	if (typeof enabled == "boolean")
	{
		//this.disabled = !enabled;
		this.overridedSetEnabled(enabled);
		
		//
		if (this.htmlControl != null)
		{
		    alert(typeof this.htmlControl);
		    this.htmlControl.disabled = !enabled;
		    this.htmlControl.style.backgroundColor = __g_StyleConstants.disabledColor;
		}
	}
};

/*  validate    */
__TextBox.validate = function(){
    if (this.mandatory && this.getSaveValue() == null)
    {
        alert(this.caption + __g_MessageConstants.mandatoryErrorMessage);
        this.htmlControl.focus();
        this.htmlControl.style.backgroundColor = __g_StyleConstants.mandatoryErrorColor;
        return false;
    }
    
    return true;
};

/*  getSaveValue    */
__TextBox.getSaveValue = function(){
	if (this.htmlControl.value.trim() == "")
	    return null;
	
	return this.htmlControl.value;
};

/*  setValue    */
__TextBox.setValue = function(value){
    this.htmlControl.value = value;
};

/*  changeViewMode  */
__TextBox.changeDisplayMode = function(mode){
    var control = this.htmlControl;
    switch (mode)
    {
        case __g_DisplayMode.Edit:
            control.className = "input";
            control.readOnly = false;
            break;
        case __g_DisplayMode.View:
            control.className = "inputreadonly";
            control.readOnly = true;
            break;
        case __g_DisplayMode.Print:
            break;
        case __g_DisplayMode.Query:
            control.className = "input";
            control.readOnly = false;
            break;
        default:
            
            break;
    }
};

// TextBox end...


/**********************************************************************************************/
/*  Number Control */
/**********************************************************************************************/
// Number Control constructor
ECCIT.Script.Web.UI.WebControls.NumberControl = function (id)
{
    this.dataLength = 0;
    this.dataScale = 0;
	ECCIT.Script.Web.UI.WebControls.TextBox.call(this, id);
}

// inherit from BaseControl
ECCIT.Script.Web.UI.WebControls.NumberControl.prototype = new ECCIT.Script.Web.UI.WebControls.TextBox();
var __NumberControl = ECCIT.Script.Web.UI.WebControls.NumberControl.prototype;

__NumberControl.getSaveValue = function(){};
__NumberControl.baseValidate = __NumberControl.validate;
__NumberControl.validate = function(){
    var valid = this.baseValidate();
    if (valid)
    {
        
    }
};


/**********************************************************************************************/
/*  DropDownList */
/**********************************************************************************************/

// DropDownList constructor
ECCIT.Script.Web.UI.WebControls.DropDownList = function (id){
    this.dataSource = null;
    this.dataValueField = null;
    this.dataTextField = null;
	ECCIT.Script.Web.UI.WebControls.BaseControl.call(this, id);
	
	var options = this.htmlControl.options;
	this.items = options;
}

// inherit from BaseControl
ECCIT.Script.Web.UI.WebControls.DropDownList.prototype = new ECCIT.Script.Web.UI.WebControls.BaseControl();
var __DropDownList = ECCIT.Script.Web.UI.WebControls.DropDownList.prototype;

/* 
    public methods of DropDownList 
    
    1. setEnabled
    2. clearItems
    3. addItem
    4. removeItem
    5. bindData
    6. changeDisplayMode
    7. validate
    8. getSaveValue
*/

/*  setEnabled  */
__DropDownList.overridedSetEnabled = __DropDownList.setEnabled;
__DropDownList.setEnabled = function(enabled){
	if (typeof enabled == "boolean")
	{
		//this.disabled = !enabled;
		this.overridedSetEnabled(enabled);
		
		//
		if (this.htmlControl != null)
		{
		    this.htmlControl.disabled = !enabled;
		    this.htmlControl.style.backgroundColor = __g_StyleConstants.disabledColor;
		}
	}
};

/*  clearItems    */
__DropDownList.clearItems = function(){
    var options = this.htmlControl.options;
    var length = options.length;
    for (var i = 0; i < length; i++){
        this.htmlControl.options.remove(0);
    }
};

/*  insertItem    */
__DropDownList.insertItem = function (index, value, text, selected) {
    var option = document.createElement("option");
    option.value = value;
    option.text = text;
    if (selected)
        option.selected = selected;

    var length = this.items.length;
    if (index < 0 || index > length) {
        this.htmlControl.options.add(option);
        return;
    }

    this.items.add(option, index);
    //this.items.add(item);
};

/*  addItem    */
__DropDownList.addItem = function (value, text, selected) {
    var option = document.createElement("option");
    option.value = value;
    option.text = text;
    if (selected)
        option.selected = selected;
    this.htmlControl.options.add(option);
    //this.items.add(item);
};

/*  selectItem    */
__DropDownList.selectItemByValue = function(value){
    var length = this.items.length;
    while (length > 0){
        if (this.items[length-1].value == value){
            this.items[length-1].selected = true;
            break;
        }
        length--;
    }
};

/*  validate    */
__DropDownList.validate = function(){
    if (this.mandatory && this.getSaveValue() == null)
    {
        alert(this.caption + __g_MessageConstants.mandatoryErrorMessage);
        this.htmlControl.focus();
        this.htmlControl.style.backgroundColor = __g_StyleConstants.mandatoryErrorColor;
        return false;
    }
    
    return true;
};

/*  getSaveValue    */
__DropDownList.getSaveValue = function(){
	if (this.htmlControl.value.trim() == "")
	    return null;
	
	return this.htmlControl.value;
};

/*  changeDisplayMode  */
__DropDownList.changeDisplayMode = function(mode){
    var control = this.htmlControl;
    switch (mode){
        case __g_DisplayMode.Edit:
        case __g_DisplayMode.Query:
            var width = control.style.width;
            var value = control.value;
            var parent = control.parentNode;
            var newControl = document.createElement("select");
            newControl.style.width = width;
            this.htmlControl = newControl;
            parent.replaceChild(newControl, control);
            
            for (var i = 0; i < this.items.length; i++)
                this.addItem(this.items[i].value, this.items[i].text);
                
            newControl.value = value;
            break;
        case __g_DisplayMode.View:
        case __g_DisplayMode.Print:
            var width = control.style.width;
            var value = control.value;
            var parent = control.parentNode;
            var newControl = document.createElement("input");
            newControl.type = "text";
            newControl.value = value;
            newControl.style.width = width;
            newControl.className = "inputreadonly";
            newControl.readOnly = true;
            this.htmlControl = newControl;
            parent.replaceChild(newControl, control);
            break;
        default:
            break;
    }
};

/*  bindData    */
__DropDownList.bindData = function(){
    // check needed parameters.
    if (this.dataSource == null){
        alert("The dataSource of DropDownList " + this.id + " is null.");
        return;
    }
    
    if (this.dataValueField == null){
        alert("The dataValueField of DropDownList " + this.id + " is null.");
        return;
    }
    
    if (this.dataTextField == null){
        alert("The dataTextField of DropDownList " + this.id + " is null.");
        return;
    }
    
    this.clearItems();
    var length = this.dataSource.length;
    for (var i = 0; i < length; i++){
        var value = eval("this.dataSource[i]." + this.dataValueField);
        var text = eval("this.dataSource[i]." + this.dataTextField); 
        this.addItem(value, text);
    }
};

/*
function getDataSourceType(dataSource)
{
	var returnValue = "";
	
	if (dataSource == null)
	    return null;

	if(typeof(dataSource) == "object" && dataSource.Tables != null && typeof(dataSource.Tables) != "undefined")
		returnValue = "DataSet";
	else if(typeof(dataSource) == "string")
		returnValue = "Xml";
	else if(typeof(dataSource) == "object" && dataSource.length != null && typeof(dataSource.length) != "undefined")
		returnValue = "IList";
	
	return returnValue;
}
*/

// DropDownList end...

/**********************************************************************************************/
/*  RadioButtonList */
/**********************************************************************************************/

// RadioButtonList constructor
ECCIT.Script.Web.UI.WebControls.RadioButtonList = function (id){
    this.dataSource = null;
    this.dataValueField = null;
    this.dataTextField = null;
	ECCIT.Script.Web.UI.WebControls.BaseControl.call(this, id);
	
	this.items = eval("document.all." + id);
	this.parents = null;
	if (this.items != null){
	    this.parents = new Array();
	    for (var i = 0; i < this.items.length; i++){
	        this.parents.push(this.items[i].parentNode);
	    }
	}
}

// inherit from BaseControl
ECCIT.Script.Web.UI.WebControls.RadioButtonList.prototype = new ECCIT.Script.Web.UI.WebControls.BaseControl();
var __RadioButtonList = ECCIT.Script.Web.UI.WebControls.RadioButtonList.prototype;

/* 
    public methods of RadioButtonList 
    
    1. setEnabled
    2. clearItems
    3. addItem
    4. removeItem
    5. bindData
    6. changeDisplayMode
    7. validate
    8. getSaveValue
*/

/*  setEnabled  */
__RadioButtonList.overridedSetEnabled = __RadioButtonList.setEnabled;
__RadioButtonList.setEnabled = function(enabled){
	if (typeof enabled == "boolean"){
		//this.disabled = !enabled;
		this.overridedSetEnabled(enabled);
		
		//
		if (this.items != null){
		    for (var i = 0; i < this.items.length; i++){
		        this.items[i].disabled = !enabled;
		    }
		}
	}
};

/*  clearItems    */
__RadioButtonList.clearItems = function () {
    if (this.items == null)
        return;
    var length = this.items.length;
    while (length > 0) {
        var parent = this.items[length - 1].parentNode;
        parent.removeChild(this.items[length - 1].nextSibling);
        parent.removeChild(this.items[length - 1]);
        length--;
    }
};

/*  addItem    */
__RadioButtonList.addItem = function (value, text, checked, index) {
    var rdo = document.createElement("input");
    rdo.type = "radio";
    rdo.name = this.id;
    rdo.value = value;
    if (checked)
        rdo.checked = checked;

    var text = document.createTextNode(text);
    //var input = document.createElement("<input type=radio name=\"" + this.id + "\" value=\"" + value + "\" checked=" + checked + ">" + text);
    this.parents[index].appendChild(rdo);
    this.parents[index].appendChild(text);
    //this.items.add(item);
};

/*  validate    */
__RadioButtonList.validate = function(){
    if (this.mandatory && this.getSaveValue() == null)
    {
        alert(this.caption + __g_MessageConstants.mandatoryErrorMessage);
        //this.htmlControl.focus();
        //this.htmlControl.style.backgroundColor = __g_StyleConstants.mandatoryErrorColor;
        return false;
    }
    
    return true;
};

/*  getSaveValue    */
__RadioButtonList.getSaveValue = function(){
    if (this.items == null || this.items.length == 0)
        return null;
        
    for (var i = 0; i < this.items.length; i++){
        if (this.items[i].checked)
            return this.items[i].value;
    }
    
    return null;
};

/*  getCheckedText    */
__RadioButtonList.getCheckedText = function () {
    if (this.items == null || this.items.length == 0)
        return null;

    for (var i = 0; i < this.items.length; i++) {
        if (this.items[i].checked)
            return this.items[i].nextSibling.nodeValue;
    }

    return null;
};

/*  setValue    */
__RadioButtonList.setValue = function (value) {
    if (this.items == null || this.items.length == 0)
        return;

    for (var i = 0; i < this.items.length; i++) {
        if (this.items[i].value == value) {
            this.items[i].checked = true;
            break;
        }
    }
};

/*  changeDisplayMode  */
__RadioButtonList.changeDisplayMode = function (mode) {
    var width = "100%";
    var value = this.getSaveValue();
    var text = this.getCheckedText();
    alert(value);
    var parent = this.parents[0];
    switch (mode) {
        case __g_DisplayMode.Edit:
        case __g_DisplayMode.Query:
            // not implemented on 2011-06-18
            break;
        case __g_DisplayMode.View:
        case __g_DisplayMode.Print:
            this.clearItems();
            var hdnControl = document.createElement("input");
            hdnControl.type = "hidden";
            hdnControl.value = value;
            parent.appendChild(hdnControl);

            var newControl = document.createElement("input");
            newControl.type = "text";
            newControl.value = text;
            newControl.style.width = width;
            newControl.className = "inputreadonly";
            newControl.readOnly = true;
            parent.appendChild(newControl);
            break;
        default:
            break;
    }
};

/*  bindData    */
// not implemented on 2011-06-18
__RadioButtonList.bindData = function(){
    // check needed parameters.
    if (this.dataSource == null){
        alert("The dataSource of RadioButtonList " + this.id + " is null.");
        return;
    }
    
    if (this.dataValueField == null){
        alert("The dataValueField of RadioButtonList " + this.id + " is null.");
        return;
    }
    
    if (this.dataTextField == null){
        alert("The dataTextField of RadioButtonList " + this.id + " is null.");
        return;
    }
    
    this.clearItems();
    var length = this.dataSource.length;
    for (var i = 0; i < length; i++){
        var value = eval("this.dataSource[i]." + this.dataValueField);
        var text = eval("this.dataSource[i]." + this.dataTextField); 
        this.addItem(value, text);
    }
};


// RadioButtonList end...